package com.hehe;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.security.reactive.ReactiveManagementWebSecurityAutoConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.cloud.security.oauth2.gateway.TokenRelayGatewayFilterFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;

@SpringBootApplication
@EnableAutoConfiguration
@Import(ReactiveManagementWebSecurityAutoConfiguration.class)
public class HeheGatewayExampleApplication {
	@Value("${example.auth.uri}")
	private String authRouter;
	@Value("${example.resource.uri}")
	private String resourceRouter;	
	@Autowired
	private TokenRelayGatewayFilterFactory filterFactory;

	public static void main(String[] args) {
		SpringApplication.run(HeheGatewayExampleApplication.class, args);
	}
	//TODO This stupid code should be replace in YML	
	@Bean
	public RouteLocator routeLocator(RouteLocatorBuilder builder) {
		return builder.routes().route(r -> r.path("/auth/**").filters(f -> f.filter(filterFactory.apply())).uri(authRouter))
				.route(r -> r.path("/resource/**").uri(resourceRouter)).build();
	}

}
